package com.wcj.study.multiredis.config;

import java.util.Map;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;

/**
 * TODO 添加类描述
 * 
 * @author 汪长军 Email：changjunwang@xinhe99.com
 * @date 2018-12-14 下午3:20:54
 * @copyright 信和上融网络科技(北京)有限公司
 */
public class JedisConnectionDisposable implements DisposableBean {
    
    public static final Logger LOGGER = LoggerFactory.getLogger(JedisConnectionDisposable.class);
    
    /**
     * 关闭连接
     *
     * @throws Exception
     */
    @Override
    public void destroy() throws Exception {
        LOGGER.info("开始停止redis.....");
        Map<String, JedisConnectionFactory> jedisConnectionFactoryMap =
            MultiRedisRegister.getJedisConnectionFactoryMap();
        if (jedisConnectionFactoryMap != null) {
            LOGGER.info( "redis instance size =============>" + jedisConnectionFactoryMap.size() );
            for (Entry<String, JedisConnectionFactory> obj : jedisConnectionFactoryMap.entrySet()) {
                JedisConnectionFactory jedisConnectionFactory = obj.getValue();
                LOGGER.info( "销毁JedisConnectionFactory,"+jedisConnectionFactory.getClientName() +","+jedisConnectionFactory.getPassword() );
                jedisConnectionFactory.destroy();
            }
        }
    }

}
